import { Body, Controller, Get, Logger, Post, Query } from "@nestjs/common";
import { AccessDictService } from "./access-dict.service";
import { ApiBasicAuth, ApiBody, ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger";
import { AccessDictType } from "./access-dict.types";
import { BusinessException } from "~/common/exceptions/biz.exception";
import { UpdateDictDto } from "./access-dict.dto";

@ApiTags("Dict - 评估字典")
@ApiBasicAuth()
@Controller('access/dict')
export class AccessDictController {
	private readonly logger = new Logger(AccessDictController.name);

	constructor(
		private readonly accessDictService: AccessDictService
	) {}

	@Get("get")
  @ApiOperation({summary: '获取评估字典列表'})
	@ApiQuery({name: "type", required: true, description: "字典类型", enum: AccessDictType})
  async getDictByType(@Query('type') type: AccessDictType) {
		try {
			return await this.accessDictService.getDictByType(type)
		}
		catch (error: any) {
			throw new BusinessException(error?.message ?? '获取评估字典列表失败')
		}
	}

	@Post("update")
	@ApiOperation({summary: '更新评估字典'})
	@ApiBody({type: UpdateDictDto})
	async updateDict(@Body() dict: UpdateDictDto) {
		try {
			// 更新字典
			return await this.accessDictService.updateDict(dict)
		}
		catch (error: any) {
			throw new BusinessException(error?.message ?? '更新评估字典失败')
		}
	}
}